<!--
 * @Descripttion:
 * @version:
 * @Author: YouHuJun
 * @Date: 2021-10-21 10:50:05
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2024-09-15 15:52:00
-->
<template>
  <div>
    <el-table
      ref="goodsTable"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="goodsList"
      border
      stripe
      highlight-current-row
      empty-text
      fit
      height="70vh"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column
        label="序号"
        type="index"
        width="55"
        align="center"
        fixed
      />
      <el-table-column
        label="详情"
        type="expand"
        align="center"
        fixed
      >
        <template slot-scope="props">
          <tableInfo :row-data="props.row" :show.sync="show" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="goods_class_name"
        label="商品分类"
        width="200"
      />
      <el-table-column
        align="center"
        prop="goods_name"
        label="商品标题"
        width="200"
      />
      <el-table-column
        align="center"
        prop="goods_code"
        label="产品编码"
        width="150"
      />
      <el-table-column
        prop="check_status"
        label="审核状态"
        width="100"
        :filters="[{ text: '待审核', value:10 },{ text: '已通过', value:30 },{ text: '未过审', value:40 }]"
        :filter-method="filterGoodsCheckStatus"
        filter-placement="bottom-end"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-show="scope.row.check_status==10" type="warning">待审核</el-tag>
          <el-tag v-show="scope.row.check_status==30" type="success">已通过</el-tag>
          <el-tag v-show="scope.row.check_status==40" type="danger">未过审</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="is_top"
        label="是否置顶"
        width="100"
        :filters="[{ text: '否', value:0 },{ text: '是', value:1 }]"
        :filter-method="filterGoodsIsTop"
        filter-placement="bottom-end"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-show="scope.row.is_top==0" type="info">否</el-tag>
          <el-tag v-show="scope.row.is_top==1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="is_recommend"
        label="是否推荐"
        width="100"
        :filters="[{ text: '否', value:0 },{ text: '是', value:1 }]"
        :filter-method="filterGoodsIsRecommend"
        filter-placement="bottom-end"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-show="scope.row.is_recommend==0" type="info">否</el-tag>
          <el-tag v-show="scope.row.is_recommend==1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="is_new"
        label="是否新品"
        width="100"
        :filters="[{ text: '否', value:0 },{ text: '是', value:1 }]"
        :filter-method="filterGoodsIsNew"
        filter-placement="bottom-end"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-show="scope.row.is_new==0" type="info">否</el-tag>
          <el-tag v-show="scope.row.is_new==1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="is_discount"
        label="是否特惠"
        width="100"
        :filters="[{ text: '否', value:0 },{ text: '是', value:1 }]"
        :filter-method="filterGoodsIsDisCount"
        filter-placement="bottom-end"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-show="scope.row.is_discount==0" type="info">否</el-tag>
          <el-tag v-show="scope.row.is_discount==1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="添加时间"
        min-width="200"
      />
      <el-table-column
        align="center"
        prop="on_sale_at"
        label="上架时间"
        min-width="200"
      />

      <el-table-column
        align="center"
        prop="sale_price"
        label="售价"
        min-width="150"
      />
      <el-table-column
        align="center"
        prop="cost_price"
        label="成本价"
        min-width="150"
      />
      <el-table-column
        align="center"
        prop="coin"
        label="系统币"
        min-width="150"
      />
      <el-table-column
        align="center"
        prop="deduct_coin"
        label="抵扣系统币"
        min-width="150"
      />

      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            el-button
            @click="handleDelete(scope.$index, scope.row)"
          >删除
            <!-- 删除(禁用)结束 -->
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import tableInfo from './tableInfo'
import { deleteGoods, multipleDeleteGoods } from '@/api/youhu-shop/V1/goods/goods'
export default
{
  // 组件名称
  name: 'GoodsTable',
  // 组件
  components:
    {
      tableInfo
    },
  props:
    {
      loading: {
        type: Boolean,
        default: true
      },
      goodsList: {
        type: Array,
        default: () =>
        {
          return []
        }
      },
      dialogVisible: {
        type: Boolean,
        default: false
      },
      dialogTitle: {
        type: String,
        default: ''
      },
      dialogType: {
        type: Number,
        default: 1
      },
      beforeGoodsForm: {
        type: Object,
        default: null
      },
      reverse: {
        type: Number,
        default: 0
      },
      multipleDelete: {
        type: Number,
        default: 0
      },
      multipleDisable: {
        type: Number,
        default: 0
      }
    },
  // 数据
  data()
  {
    return {
      show: 1,
      baseUrl: '',
      // 选中队象的数组
      multipleSelection: [],
      // 选中元素的下标
      selectIndex: [],
      // 反选元素的下标
      diffIndex: [],
      // 选中的下标
      selectId: []
    }
  },
  // 计算属性
  computed:
    {
    },
  // 监听
  watch:
    {
      reverse(value)
      {
        this.diffIndex = []

        const all = []

        for (let index = 0; index < this.goodsList.length; index++)
        {
          all.push(index)
        }

        this.diffIndex = all.filter(val =>
        {
          return !this.selectIndex.includes(val)
        })

        // 实现反选的效果
        this.$refs.goodsTable.clearSelection()
        this.diffIndex.forEach(index =>
        {
          this.$refs.goodsTable.toggleRowSelection(this.goodsList[index])
        })

        this.selectIndex = this.diffIndex
      },
      // 批量删除
      multipleDelete(value)
      {
        this.selectId = []
        this.getSelectId()
        const selectId = {}
        selectId['selectId'] = this.selectId
        const _this = this
        this.$confirm('确认要批量删除商品吗？', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '删除',
          cancelButtonText: '取消'
        })
          .then(() =>
          {
            multipleDeleteGoods(selectId).then((res) =>
            {
              if (res && res.code === 0)
              {
                _this.$emit('hanldeUpdateData', true)
                _this.$message(
                  {
                    message: res.msg,
                    type: 'success'
                  })
              }
            })
          })
          .catch(action =>
          {
            return
          })
      }
    },
  // 实例创建之前
  beforeCreate()
  {
  },
  // 创建
  created()
  {
  },
  // 挂载之前
  beforeMount()
  {
  },
  // 挂载 --常用
  mounted()
  {
    this.baseUrl = process.env.VUE_APP_URL
  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {

    /**
     * 过滤商品审核状态
     */
    filterGoodsCheckStatus(value, row)
    {
      return row.checkStatus === value
    },
    /**
     * 过滤商品置顶状态
     */
    filterGoodsIsTop(value, row)
    {
      return row.is_top === value
    },
    /**
     * 过滤商品推荐状态
     */
    filterGoodsIsRecommend(value, row)
    {
      return row.is_recommend === value
    },
    /**
     * 过滤商品新品状态
     */
    filterGoodsIsNew(value, row)
    {
      return row.is_new === value
    },
    /**
     * 过滤商品新品优惠状态
     */
    filterGoodsIsDisCount(value, row)
    {
      return row.is_discount === value
    },

    // 修改管理员
    handleEdit(index, row)
    {
      this.$router.push({ path: 'editGoods', query: { goods: row }})
    },

    // 删除
    handleDelete(index, row)
    {
      const param = {}
      param['id'] = row.id
      param['is_delete'] = 1

      this.deleteGoods(param, index)
    },

    // 删除
    deleteGoods(param, index)
    {
      const _this = this
      this.$confirm('确认要删除该商品吗？', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '删除',
        cancelButtonText: '取消'
      })
        .then(() =>
        {
          deleteGoods(param).then((res) =>
          {
            if (res && res.code === 0)
            {
              _this.$message(
                {
                  message: res.msg,
                  type: 'success'
                })
              _this.$emit('hanldeUpdateData', true)
            }
          })
        })
        .catch(action =>
        {
          return
        })
    },
    // 多选
    handleSelectionChange(val)
    {
      this.multipleSelection = val

      this.selectIndex = []
      this.multipleSelection.forEach(item =>
      {
        this.selectIndex.push(this.goodsList.indexOf(item))
      })
    },
    // 获取选中下标
    getSelectId()
    {
      this.selectIndex.forEach(item =>
      {
        this.selectId.push(this.goodsList[item].id)
      })
    }
  }

}
</script>
<style lang='scss' scoped>
</style>
